/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Oct 9, 2013 10:46:29 AM
 */
package com.googlecode.stereoplot;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.plot.dial.DialPlot;

/**
 * @author lianyuy
 * 
 */
public class StereoPlotSelectionLayer extends StereoGridLayer implements
		ChartMouseListener, ChartMouseDragListener {
	protected StereoPlotSelectionMode selectionMode;
	protected double azimuth;
	protected double inclination;
	protected double radiusRatio = Double.NaN;

	private int location_x, location_y;

	/**
	 * 
	 */
	public StereoPlotSelectionLayer() {
		super();
		this.selectionMode = StereoPlotSelectionMode.AzimuthMode;
	}

	/**
	 * @param selectionMode
	 */
	public StereoPlotSelectionLayer(StereoPlotSelectionMode selectionMode) {
		this();
		this.selectionMode = selectionMode;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jfree.chart.plot.dial.DialLayer#draw(java.awt.Graphics2D,
	 * org.jfree.chart.plot.dial.DialPlot, java.awt.geom.Rectangle2D,
	 * java.awt.geom.Rectangle2D)
	 */
	@Override
	public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
			Rectangle2D view) {
		Rectangle2D nintyDegreeRectInner = DialPlot.rectangleByRadius(frame,
				StereoGridLayer.NintyDegreeRadius,
				StereoGridLayer.NintyDegreeRadius);
		Arc2D arcNintyyDegree = new Arc2D.Double(nintyDegreeRectInner, 0, 360,
				Arc2D.OPEN);

		g2.setPaint(this.paint);
		g2.setStroke(new BasicStroke(2.0f));

		if (plot instanceof StereoDialPlot) {
			Area area = new Area(arcNintyyDegree);
			double width = area.getBounds2D().getWidth();
			double height = area.getBounds2D().getHeight();
			double offsetx = location_x - nintyDegreeRectInner.getX();
			double offsety = location_y - nintyDegreeRectInner.getY();

			double x = offsetx - width / 2;
			double y = offsety - height / 2;
			double beta = height / width;
			double theta = Math.atan2(y, beta * x);
			double a = x / Math.cos(theta);

			double deviate = a / width * Math.PI;
			if (deviate > Math.PI / 2)
				return;

			// double azimuth = Math.PI / 2 - theta;

			double ratio = deviate / (Math.PI / 2)
					* StereoGridLayer.NintyDegreeRadius;
			Rectangle2D rectInner = DialPlot.rectangleByRadius(frame, ratio,
					ratio);
			Arc2D arc = new Arc2D.Double(rectInner, 0, 360, Arc2D.OPEN);
			g2.draw(arc);
		}

		if (!Double.isNaN(radiusRatio)) {
			g2.setPaint(this.paint);
			g2.setStroke(new BasicStroke(1.0f));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jfree.chart.ChartMouseListener#chartMouseClicked(org.jfree.chart.
	 * ChartMouseEvent)
	 */
	@Override
	public void chartMouseClicked(ChartMouseEvent evt) {
		location_x = evt.getTrigger().getX();
		location_y = evt.getTrigger().getY();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jfree.chart.ChartMouseListener#chartMouseMoved(org.jfree.chart.
	 * ChartMouseEvent)
	 */
	@Override
	public void chartMouseMoved(ChartMouseEvent evt) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.stereoplot.ChartMouseDragListener#chartMouseDragged(com
	 * .googlecode.stereoplot.ChartMouseDragEvent)
	 */
	@Override
	public void chartMouseDragged(ChartMouseDragEvent evt) {
	}

}
